package thread;

//  synchronized : 直接修饰代码块
public class demo14_synchronized {
    private static long count = 0;
    private static Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            //  循环 5W 次
            /*for (int i = 0; i < 50000; i++) {
                synchronized(lock) {
                    count++;
                }
            }*/

            //  循环一次
            synchronized (lock) {
                for (int i = 0; i < 50000; i++) {
                    count++;
                }
            }
        });

        Thread t2 = new Thread(() -> {
            //  循环 5W 次
            /*for (int i = 0; i < 50000; i++) {
                synchronized(lock) {
                    count++;
                }
            }*/

            //  循环一次
            synchronized (lock) {
                for (int i = 0; i < 50000; i++) {
                    count++;
                }
            }

        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(count);

    }
}